﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using evt;
using tools;
public class ZJHFace : MonoBehaviour {

	// Use this for initialization
	public static Dictionary<string,Sprite> dic = new Dictionary<string,Sprite>();
	private GameObject Faces = null;
	private GameObject TxtInput = null;
	private InputField input = null;
	void Start () {
		
		Faces = transform.GetChild (0).gameObject;
		TxtInput = transform.GetChild (1).gameObject;

		EventTriggerListener.Get (gameObject).onClick = HideView;
		Transform tran = Faces.transform;
		for( int i = 0 ; i < tran.childCount ; i++ ){
			EventTriggerListener.Get (tran.GetChild(i).gameObject).onClick = UseFace;
			tran.GetChild(i).gameObject.name = i.ToString ();
			dic [tran.GetChild(i).gameObject.name] = tran.GetChild (i).GetComponent<Image> ().sprite;
		}

		tran = TxtInput.transform;
		for( int s = 0 ; s < 11 ; s++ ){
			EventTriggerListener.Get (tran.GetChild(s).gameObject).onClick = UseFastLanguage;
		}

		input = tran.GetChild (11).GetChild (0).GetComponent<InputField> ();
		EventTriggerListener.Get (tran.GetChild(12).gameObject).onClick = SendInputText;

		EventTriggerListener.Get (transform.GetChild(2).gameObject).onClick = ShowFace;
		EventTriggerListener.Get (transform.GetChild(3).gameObject).onClick = ShowTextInput;

		ShowFace (null);
	}

	private void ShowTextInput(GameObject go){
		Faces.gameObject.SetActive (false);
		TxtInput.gameObject.SetActive (true);
		transform.GetChild (2).GetChild (0).gameObject.SetActive (false);
		transform.GetChild (2).GetChild (1).gameObject.SetActive (true);

		transform.GetChild (3).GetChild (0).gameObject.SetActive (true);
		transform.GetChild (3).GetChild (1).gameObject.SetActive (false);
	}

	private void ShowFace(GameObject go){
		Faces.gameObject.SetActive (true);
		TxtInput.gameObject.SetActive (false);
		transform.GetChild (2).GetChild (0).gameObject.SetActive (true);
		transform.GetChild (2).GetChild (1).gameObject.SetActive (false);

		transform.GetChild (3).GetChild (0).gameObject.SetActive (false);
		transform.GetChild (3).GetChild (1).gameObject.SetActive (true);
	}

	private void HideView(GameObject go){
		transform.localPosition = new Vector3 (0, 10000, 0);
	}

	private void UseFace(GameObject go){
		GSocket.Send (ByteFactory.makeByte (102101, "1,"+go.name));
		HideView (null);
	}

	private void UseFastLanguage(GameObject go){
		GSocket.Send (ByteFactory.makeByte (102101, "2,"+go.name+","+go.GetComponent<Text>().text));
		HideView (null);
	}

	private void SendInputText(GameObject go){
		if ( input.text.Trim ().Length > 0 ) {
			GSocket.Send (ByteFactory.makeByte (102101, "3,"+input.text));
			input.text = "";
			HideView (null);
		}

	}

	
	// Update is called once per frame
	void Update () {
	
	}
}
